/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.facade.employment;

import com.xtd.model.employment.Employee;
import com.xtd.model.employment.Payroll;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.EntityManager;

/**
 *
 * @author Lu Yuanbin
 */
public class ShootingPayrollCalculator extends PayrollCalculator {

    @Override
    public Payroll calculatePayroll(EntityManager em, Employee employee, Date startDate, Date endDate, Employee createdBy) {
        Payroll payroll = new Payroll(employee, startDate, endDate, createdBy);



        BigDecimal royalty = (BigDecimal) em.createQuery("select sum((os.orderCommodity.quantity-os.orderCommodity.returnedQuantity) * os.quantityRoyalty + ((os.orderCommodity.quantity-os.orderCommodity.returnedQuantity)* os.orderCommodity.unitPrice)*(os.priceRoyalty-os.introducerRoyalty))"
                + "from OrderServer as os where os.server = :employee and "
                + "os.settledTime between :startDate and :endDate ").setParameter("employee", employee).setParameter("startDate", startDate).setParameter("endDate", endDate).getSingleResult();
        if (royalty == null) {
            royalty = BigDecimal.ZERO;
        }

        payroll.setRoyalty(royalty.add(calculateIntrudoctionRoyalty(em, employee, startDate, endDate)));
        return payroll;
    }

    public ShootingPayrollCalculator() {
    }
}
